package antidestiny.utilib;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;


/**
 * Created by Hero on 2015/8/19.
 */
public class BitmapUtils {
    float thumbnailWidth =400;
    float thumbnailHeight =800;

    public float getThumbnailWidth() {
        return thumbnailWidth;
    }

    public float getThumbnailHeight() {
        return thumbnailHeight;
    }

    public BitmapUtils(){}
    public BitmapUtils(float recommendWidth){
        this.thumbnailWidth =recommendWidth;
        thumbnailHeight =recommendWidth/1.73f;
    }
    public Bitmap thumbnail(File file){

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;

        BitmapFactory.decodeFile(file.getPath(),options);

            if (options.outWidth > thumbnailWidth ||options.outHeight> thumbnailHeight) {
                float his=options.outHeight/ thumbnailHeight;
                float wis=options.outWidth/ thumbnailWidth;
                options.inSampleSize = (int)(his>wis?his:wis);
                options.inJustDecodeBounds = false;
                return BitmapFactory.decodeFile(file.getPath(),options);
            } else {
                return BitmapFactory.decodeFile(file.getPath());
            }
    }

    public Bitmap resizeBitmap(Bitmap bitmap){
        if (thumbnailWidth <=0){
            Aide.t("时机不合适，无法使用合适的图片大小：大小为" + thumbnailWidth);
        }
        int w= (int) thumbnailWidth;
        int h=  (int)thumbnailHeight;
        float ow=bitmap.getWidth();
        float oh=bitmap.getHeight();
        if (oh>h||ow>w){
            float hs=oh/h;
            float ws=ow/w;
            float s=hs>ws?hs:ws;
            Aide.t("处理后图片的宽度："+(ow/s)+"缩放倍数是："+s+"原图片大小是w"
                    +bitmap.getWidth()+"h"
                    +bitmap.getHeight()+
                    "textview 的宽度是："+ thumbnailWidth);
            return  Bitmap.createScaledBitmap(bitmap, (int)(ow/s)
                    , (int)(oh/s), false);

        }else return  bitmap.copy(Bitmap.Config.ARGB_8888,false);
    }


}
